package cn.ffcs.unm.insurance.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;

import cn.ffcs.unm.insurance.dao.InsuranceTypeDao;
import cn.ffcs.unm.insurance.model.BmData;
import cn.ffcs.unm.insurance.model.InsurancePeriod;
import cn.ffcs.unm.insurance.model.InsuranceProject;
import cn.ffcs.unm.insurance.model.InsurancePublicProject;
import cn.ffcs.unm.insurance.model.InsuranceState;
import cn.ffcs.unm.insurance.model.InsuranceType;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("insuranceTypeDao")
public class InsuranceTypeDaoImpl extends BaseDaoImpl
		implements
			InsuranceTypeDao {

	@Override
	public PageInfo queryPageInfoByInsuranceType(InsuranceType insuranceType,
			int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from VIEW_BM_BMCLASS ");
		List<Object> params = new ArrayList<Object>();
		if (null != insuranceType) {
			if (!StrUtil.isEmpty(insuranceType.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(insuranceType.getMonthId());
			}
			if (!StrUtil.isEmpty(insuranceType.getName())) {
				sb.append(" AND JA1.NAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insuranceType.getName())
						+ "%");
			}
			if (!StrUtil.isEmpty(insuranceType.getCode())) {
				sb.append(" AND JA1.CODE LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insuranceType.getCode())
						+ "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, InsuranceType.class);
	}

	@Override
	public PageInfo queryPageInfoByInsuranceProject(
			InsuranceProject insuranceProject, int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from VIEW_BM_CLASSITEM where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != insuranceProject) {
			if (!StrUtil.isEmpty(insuranceProject.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(insuranceProject.getMonthId());
			}
			if (!StrUtil.isEmpty(insuranceProject.getName())) {
				sb.append(" AND NAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insuranceProject
								.getName()) + "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, InsuranceProject.class);
	}

	@Override
	public PageInfo queryPageInfoByInsurancePublicProject(
			InsurancePublicProject insurancePublicProject, int currentPage,
			int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from view_BM_ITEM where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != insurancePublicProject) {
			if (!StrUtil.isEmpty(insurancePublicProject.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(insurancePublicProject.getMonthId());
			}
			if (!StrUtil.isEmpty(insurancePublicProject.getName())) {
				sb.append(" AND NAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insurancePublicProject
								.getName()) + "%");
			}
			if (!StrUtil.isEmpty(insurancePublicProject.getCode())) {
				sb.append(" AND CODE LIKE ? ");
				params.add(insurancePublicProject.getCode());
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, InsurancePublicProject.class);
	}

	@Override
	public PageInfo queryPageInfoByInsurancePeriod(
			InsurancePeriod insurancePeriod, int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from view_BM_PERIOD where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != insurancePeriod) {
			if (!StrUtil.isEmpty(insurancePeriod.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(insurancePeriod.getMonthId());
			}
			if (!StrUtil.isEmpty(insurancePeriod.getPkPeriodscheme())) {
				sb.append(" AND  pkPeriodscheme LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insurancePeriod
								.getPkPeriodscheme()) + "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, InsurancePeriod.class);
	}

	@Override
	public PageInfo queryPageInfoByInsuranceState(
			InsuranceState insuranceState, int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from view_BM_PERIODSTATE where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != insuranceState) {
			if (!StrUtil.isEmpty(insuranceState.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(insuranceState.getMonthId());
			}
			if (!StrUtil.isEmpty(insuranceState.getPkBmClass())) {
				sb.append(" AND pkBmClass LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(insuranceState.getPkBmClass())
						+ "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, InsuranceState.class);
	}

	@Override
	public PageInfo queryPageInfoByBmData(BmData bmData, int currentPage,
			int pageSize) {
		StringBuilder sb = new StringBuilder(
				" select * from VIEW_BM_DATA where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != bmData) {
			if (!StrUtil.isEmpty(bmData.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(bmData.getMonthId());
			}
			if (!StrUtil.isEmpty(bmData.getPkPsndoc())) {
				sb.append(" AND pkpsndoc LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(bmData.getPkPsndoc())
						+ "%");
			}
			if (!StrUtil.isEmpty(bmData.getBmaccountno())) {
				sb.append(" AND BMACCOUNTNO = ? ");
				params.add(bmData.getBmaccountno());
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, BmData.class);
	}

}
